package gemini.castor.ui.client.page.navigation;

import gemini.basic.model.User;
import gemini.castor.ui.client.mvp.event.ContainerRequestEvent;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.utils.PlaceConstants;
import gemini.castor.ui.client.utils.PlaceConstants.MenuItem;
import gemini.castor.ui.client.utils.PlaceConstants.NavigationItem;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class NavigationWidget extends Composite {

	private VerticalPanel pnlContainer;
	private HTML currentNavHtm;
	private HTML currentNavHtmMove;
	private NavigationWidgetConstants constants;
	private User user;

	public NavigationWidget(MenuItem _menuItem, User user) {
		constants = GWT.create(NavigationWidgetConstants.class);
		this.user = user;
		initCustom(_menuItem);
		initWidget(pnlContainer);

		CastorGinjector.INSTANCE.getEventBus().addHandler(
				NavigationEvent.getType(), new NavigationHandler() {

					@Override
					public void onClick(NavigationEvent event) {
						clickNavigation(event.getItem());
					}

					@Override
					public void onSelected(NavigationEvent event) {
						selectNavigation(event.getItem());
					}
				});

	}
	
	private boolean home = false;
	
	private void initCustom(MenuItem _menuItem) {
		
		if (_menuItem.equals(MenuItem.HOME)) {
			home = true;
			navItems = PlaceConstants.homeNavigationItems;
		} else if (_menuItem.equals(MenuItem.ACCOUNT)) {
			navItems = PlaceConstants.accountNavigationItems;
		} else if (_menuItem.equals(MenuItem.OFFICE)) {
			navItems = PlaceConstants.officeNavigationItems;
		} else if (_menuItem.equals(MenuItem.CUSTOMER)) {
			navItems = PlaceConstants.customerNavigationItems;
		} else if (_menuItem.equals(MenuItem.TRAINING)) {
			navItems = PlaceConstants.trainingNavigationItems;
		} else if (_menuItem.equals(MenuItem.STORE)) {
			navItems = PlaceConstants.storeNavigationItems;
		}

		pnlContainer = new VerticalPanel();
		for (NavigationItem navItem : navItems) {
			pnlContainer.add(createNavHtm(navItem));
		}
	}

	private List<NavigationItem> navItems;

	private HTML createNavHtm(final NavigationItem navItem) {
		HTML navHtm = new HTML(constants.getString(navItem.getTextKey()));
		if(user == null&& !home){
			navHtm.setStyleName("htmItemDisabled");
		} else {
			navHtm.addStyleName("htmItem");
			navHtm.addClickHandler(new ClickHandler() {
	
				@Override
				public void onClick(ClickEvent event) {
					clickNavigation(navItem);
				}
			});
		}
		
		navHtm.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (currentNavHtmMove != null) {
					currentNavHtmMove.removeStyleName("htmItemHover");
				}
				currentNavHtmMove = (HTML) event.getSource();
				if (currentNavHtmMove != currentNavHtm) {
					currentNavHtmMove.addStyleName("htmItemHover");
				}
			}
		});
		
		navHtm.addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(MouseOutEvent event) {
				if (currentNavHtmMove != null) {
					currentNavHtmMove.removeStyleName("htmItemHover");
				}
			}
		});
		
		return navHtm;
	}

	public void setCurrentNavHtm(HTML htm) {
		if (currentNavHtm != null) {
			currentNavHtm.removeStyleName("htmNav");
		}
		currentNavHtm = htm;
		currentNavHtm.addStyleName("htmNav");
		
		if (currentNavHtmMove != null) {
			currentNavHtmMove.removeStyleName("htmItemHover");
		}
	}
	
	public void selectNavigation(NavigationItem navItem) {
		HTML htmNav;
		for (int i = 0; i < pnlContainer.getWidgetCount(); i++) {
			htmNav = (HTML) pnlContainer.getWidget(i);
			if (htmNav.getHTML().equals(
					constants.getString(navItem.getTextKey()))) {
				setCurrentNavHtm(htmNav);
			}
		}
	}

	public void clickNavigation(NavigationItem navItem) {
		selectNavigation(navItem);

		CastorGinjector.INSTANCE.getEventBus().fireEvent(
				new ContainerRequestEvent(navItem.getPlaceId()));
	}
}
